package book.chapter07.$7_1_5;

import book.util.Constant;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

//使用Super权限模式进行权限控制
public class AuthSample_Super {

    final static String PATH = "/zk-book";

    /**
     * 本例子失败：因为需要预先设置 super 参数。
     * 要使用超级管理员权限，首先需要在zk 服务器开启Super 模式：zk 启动的时候，添加如下系统属性：
     * -Dzookeeper.DigestAuthenticationProvider.superDigest=foo:kWN6aNSbjcKWPqjiV7cg0N24raU=
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        ZooKeeper zookeeper1 = new ZooKeeper(Constant.CONN_STR, 5000, null);
        zookeeper1.addAuthInfo("digest", "foo:true".getBytes());
        zookeeper1.create(PATH, "init".getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.EPHEMERAL);

        ZooKeeper zookeeper2 = new ZooKeeper(Constant.CONN_STR, 50000, null);
        //zookeeper2.addAuthInfo("digest", "foo:zk-book".getBytes());
        //想成功，必须先在服务端设置 foo:zk-book 的加密配置
        zookeeper2.addAuthInfo("digest", "foo:zk-book".getBytes());
        System.out.println(zookeeper2.getData(PATH, false, null));

        ZooKeeper zookeeper3 = new ZooKeeper(Constant.CONN_STR, 50000, null);
        zookeeper3.addAuthInfo("digest", "foo:false".getBytes());
        System.out.println(zookeeper3.getData(PATH, false, null));
    }
}